package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.DepartmentDto;
import primetest.faces.DepartmentListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.DepartmentService;
import primetest.impl.service.DoctorService;

@Named("departmentListControler")
@SessionScoped
@RolesAllowed({"department", "admin"})
public class DepartmentListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -118678570623180842L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/*
	 * 'Conversation' object required in order to start a conversation scope See
	 * boolList.xhtml
	 */
	@Inject
	private SessionManager sessionManager;


	private DepartmentListModel departmentList = null;
	private List<DepartmentDto> baseDepartmentList;
	private DepartmentDto selectedDepartment = new DepartmentDto();
	private DepartmentDto backDepartment = new DepartmentDto();
	private DepartmentDto newDepartment = new DepartmentDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;
	private List<DepartmentDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private DepartmentService departmentService;

	@Inject
	private DoctorService doctorService;

	@RolesAllowed({"department", "admin"})
	public DepartmentListModel getDepartmentList() {
		if (baseDepartmentList == null) {
			try {
				baseDepartmentList = departmentService.getDepartmentList();
				departmentList = new DepartmentListModel(baseDepartmentList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseDepartmentList == null ? new DepartmentListModel(
				new ArrayList<DepartmentDto>()) : departmentList;
	}

	public List<DepartmentDto> getBaseDepartmentList() {
		if (baseDepartmentList == null) {
			try {
				baseDepartmentList = departmentService.getDepartmentList();
				departmentList = new DepartmentListModel(baseDepartmentList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return baseDepartmentList;
	}

	public void deleteDepartment() throws IOException {
		if (doctorService.getDoctorListByDepartment(selectedDepartment.getSeq()).size() == 0) {
			departmentService.deleteDepartment(selectedDepartment.getSeq());
		} else {
			sessionManager.addGlobalMessageFatal("The deparment still has some doctors", null);
		}
	}

	public DepartmentDto getSelectedDepartment() {
		return selectedDepartment;
	}

	public void setSelectedDepartment(DepartmentDto selectedDepartment) {
		this.selectedDepartment = selectedDepartment;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}


	public DepartmentDto getNewDepartment() {
		return newDepartment;
	}

	public void setNewDepartment(DepartmentDto newDepartment) {
		this.newDepartment = newDepartment;
	}

	public void startEditDepartment() {
		editMode = true;
		backDepartment.setAll(selectedDepartment);
	}

	public void saveEditDepartment() {
		try {
			if (selectedDepartment.isValid()) {
				departmentService.updateDepartment(selectedDepartment);
				logger.debug("save the selectedDepartment" + " name: "
						+ selectedDepartment.getName() + " seq: "
						+ selectedDepartment.getSeq());
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("BuildDate invalid", null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void cancelEditDepartment() {
		editMode = false;
		selectedDepartment.setAll(backDepartment);
	}

	public void startAddDepartment() {
		addMode = true;
		newDepartment = new DepartmentDto();
	}

	public void addNewDepartment() throws IOException {
		if (newDepartment.isValid()) {
			departmentService.addDepartment(newDepartment);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("Build Date invalid", null);
		}
	}

	public void cancelAddDepartment() {
		addMode = false;
		newDepartment = new DepartmentDto();
	}

	public List<DepartmentDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<DepartmentDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}
}
